import { requst } from "@/utils"
export const getDetails = () => {
  //获取文章类型列表
  return async(dispatch) => {
    const res = await requst.get('/channels')
    dispatch({type:'details/getdetails',payload:res.data.data.channels})
  }
}
//获取文章数据列表
export const getlist = (prpos) => {
  return async(dispatch) => {
    console.log(prpos);
    const queryParams = {}
    if (prpos.status !== -1) {
      queryParams.status = prpos.status
    }
    if (prpos.channelId !== -1) {
      queryParams.channel_id = prpos.channelId
    }
    if (prpos.dateStart) {
      queryParams.begin_pubdate=prpos.dateStart
    }
    if (prpos.dateEnd) {
      queryParams.end_pubdate=prpos.dateEnd
    }
      queryParams.page=prpos.page 
      queryParams.pageSize=prpos.pageSize
    const res = await requst.get('/mp/articles', { params: queryParams })
    console.log(res);
    const list = res.data.data.results
    const total=res.data.data.total_count
    dispatch({
      type: 'details/queryParams', payload: {
        list: list.map(item=>{return{...item,cover:item.cover.images[0]}}),
        total:total
    }})
  }
}
//删除数据
export const remList = (id,props) => {
  return (dispatch) => {
    requst.delete(`/mp/articles/${id}`)
    dispatch(getlist(props))
  }
}
//发表文章
export const submitAction = (data) => {
  return async (dispatch) => {
    console.log(data);
    const newData = {
      title: data.values.title,
      content: data.values.content,
      channel_id: data.values.channelID,
      cover: {
        type: data.values.type,
        images:data.fileList.map(item=>item.response.data.url)
      }
    }
    await requst.post('/mp/articles?draft=false',newData)
  }
}